﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using XXVIII.Api.Models;
using XXVIII.BLL;
using XXVIII.Entity;

namespace XXVIII.Api.Controllers
{
    /// <summary>
    /// 表单模块
    /// </summary>
    public class FormController : ApiController
    {
        /// <summary>
        /// 创建 表结构
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultDto<dynamic> CreateSignTable(GeneralCreateTableRequest obj)
        {
            ResultDto<dynamic> result = new ResultDto<dynamic>();
            try
            {
                if (!Pickout.CheckTableColumnName(obj.TableName))
                {
                    result.FirstParam = false;
                    result.Status = 201;
                    result.Message = "TableName" + obj.TableName + " Invalid";
                }
                else if (obj == null || obj.Columns.Count == 0)
                {
                    result.FirstParam = false;
                    result.Status = 202;
                    result.Message = "Columns Empty";
                }

                if (FormBLL.Instance.CheckTableExist(obj.TableName))
                {
                    result.FirstParam = false;
                    result.Status = 203;
                    result.Message = "Table Already Exist";
                }

                List<CreateCloumnEntity> columns = new List<CreateCloumnEntity>();

                foreach (var item in obj.Columns)
                {
                    if (!Pickout.CheckTableColumnName(item.ColumnName))
                    {
                        result.FirstParam = false;
                        result.Status = 204;
                        result.Message = "ColumnName" + item.ColumnName + " Invalid";
                        return result;
                    }
                    CreateCloumnEntity column = new CreateCloumnEntity();
                    column.ColumnName = item.ColumnName;
                    column.ColumnType = item.ColumnType;
                    column.Length = item.Length;
                    column.DecimalLength = item.DecimalLength;
                    column.IsIdentity = item.IsIdentity;
                    column.IsNULL = item.IsNULL;
                    column.IsKey = item.IsKey;
                    column.IdentitySeed = item.IdentitySeed;
                    column.IdentityIncrement = item.IdentityIncrement;
                    column.DefualtValue = item.DefualtValue;
                    column.Describe = item.Describe;
                    columns.Add(column);
                }
                var data = FormBLL.Instance.CreateSignTable(obj.TableName, columns);
                if (data)
                {
                    var json=Newtonsoft.Json.JsonConvert.SerializeObject(columns);
                    //记录表信息
                    FormBLL.Instance.RecordToTable(obj.TableName,json);
                }
                result.FirstParam = data;
                result.Status = 1;
                result.Message = "Requst Is Success";
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/Form/CreateSignTable Server Error";
            }
            return result;
        }


        /// <summary>
        /// 获取 表结构及核心属性
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultDto<dynamic> GetTableProperty(GeneralTablePropertyRequest obj)
        {
            ResultDto<dynamic> result = new ResultDto<dynamic>();
            try
            {
                if (!Pickout.CheckTableColumnName(obj.TableName))
                {
                    result.FirstParam = false;
                    result.Status = 201;
                    result.Message = "TableName" + obj.TableName + " Invalid";
                }
                var data = FormBLL.Instance.GetTableProperty(obj.TableName);
                result.FirstParam = data;
                result.Status = 1;
                result.Message = "Requst Is Success";
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/Form/GetTableProperty Server Error";
            }
            return result;
        }

        /// <summary>
        /// 移除 表结构
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultDto<dynamic> DropSignTable(GeneralTablePropertyRequest obj)
        {
            ResultDto<dynamic> result = new ResultDto<dynamic>();
            try
            {

                if (!Pickout.CheckTableColumnName(obj.TableName))
                {
                    result.FirstParam = false;
                    result.Status = 201;
                    result.Message = "TableName" + obj.TableName + " Invalid";
                }
                var data = FormBLL.Instance.DropSignTable(obj.TableName);
                //移除表记录信息
                if (data)
                {
                    FormBLL.Instance.UnRecordToTable(obj.TableName);
                }

                result.FirstParam = data;
                result.Status = 1;
                result.Message = "Requst Is Success";
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/Form/DropTable Server Error";
            }
            return result;
        }


        /// <summary>
        /// 获取列表 分页
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultDto<dynamic, dynamic> GetByPage(GeneralSearchRequest obj)
        {
            ResultDto<dynamic, dynamic> result = new ResultDto<dynamic, dynamic>();
            try
            {
                if (string.IsNullOrEmpty(obj.TableName))
                {
                    obj.TableName = "SYS_TABLE";
                }
                if (!Pickout.CheckTableColumnName(obj.TableName))
                {
                    result.FirstParam = false;
                    result.Status = 201;
                    result.Message = "TableName" + obj.TableName + " Invalid";
                    return result;
                }

                DBPageBase page = Pickout.SetPage(obj);
                List<TableAndOrLikeEntity> andOrLikeOrderList = new List<TableAndOrLikeEntity>();
                if (obj.AndOrLikeList != null && obj.AndOrLikeList.Count>0)
                {
                    foreach (var item in obj.AndOrLikeList)
                    {
                        if (!Pickout.CheckTableColumnName(item.ColumnName))
                        {
                            result.FirstParam = false;
                            result.Status = 204;
                            result.Message = "ColumnName" + item.ColumnName + " Invalid";
                            return result;
                        }
                        TableAndOrLikeEntity andOrLikeOrder = new TableAndOrLikeEntity();
                        andOrLikeOrder.AndOrLikeType = item.AndOrLikeType;
                        andOrLikeOrder.ColumnName = item.ColumnName;
                        andOrLikeOrder.Value = item.Value;
                        andOrLikeOrderList.Add(andOrLikeOrder);
                    }
                }
                var data = FormBLL.Instance.GetByPage(obj.TableName, andOrLikeOrderList, ref page);
                result.FirstParam = data;
                result.SecondParam = page.TotalRows;
                result.Status = 1;
                result.Message = "Requst Is Success";
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/Form/GetByPage Server Error";
            }
            return result;
        }

        /// <summary>
        /// 增加 数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultDto<dynamic> Add(GeneralAddRequest obj)
        {
            ResultDto<dynamic> result = new ResultDto<dynamic>();
            try
            {
                if (!Pickout.CheckTableColumnName(obj.TableName))
                {
                    result.FirstParam = false;
                    result.Status = 201;
                    result.Message = "TableName" + obj.TableName + " Invalid";
                }
                else if (obj == null || obj.Columns.Count == 0)
                {
                    result.FirstParam = false;
                    result.Status = 202;
                    result.Message = "Columns  Empty";
                }
                Dictionary<string, string> keyValue = new Dictionary<string, string>();
                foreach (var item in obj.Columns)
                {
                    if (!Pickout.CheckTableColumnName(item.ColumnName))
                    {
                        result.FirstParam = false;
                        result.Status = 203;
                        result.Message = "ColumnName" + item.ColumnName + " Invalid";
                        return result;
                    }
                    keyValue.Add(item.ColumnName, item.ColumnValue);
                }
                


                var data = FormBLL.Instance.Add(obj.TableName, keyValue);
                result.Status = 1;
                result.Message = "Requst Is Success";
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/Form/Add Server Error";
            }
            return result;
        }

        /// <summary>
        /// 删除 数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultDto<dynamic> Delete(GeneralDeleteRequest obj)
        {
            ResultDto<dynamic> result = new ResultDto<dynamic>();
            try
            {
                if (!Pickout.CheckTableColumnName(obj.TableName))
                {
                    result.FirstParam = false;
                    result.Status = 201;
                    result.Message = "TableName" + obj.TableName + " Invalid";
                }
                else if (obj == null || obj.AndOrLikeList.Count == 0)
                {
                    result.FirstParam = false;
                    result.Status = 202;
                    result.Message = "AndOrLike Empty";
                }
                List<TableAndOrLikeEntity> andOrLikeOrderList = new List<TableAndOrLikeEntity>();
                foreach (var item in obj.AndOrLikeList)
                {
                    if (!Pickout.CheckTableColumnName(item.ColumnName))
                    {
                        result.FirstParam = false;
                        result.Status = 204;
                        result.Message = "ColumnName" + item.ColumnName + " Invalid";
                        return result;
                    }
                    TableAndOrLikeEntity andOrLikeOrder = new TableAndOrLikeEntity();
                    andOrLikeOrder.AndOrLikeType = item.AndOrLikeType;
                    andOrLikeOrder.ColumnName = item.ColumnName;
                    andOrLikeOrder.Value = item.Value;
                    andOrLikeOrderList.Add(andOrLikeOrder);
                }
                var data = FormBLL.Instance.Delete(obj.TableName, andOrLikeOrderList);
                result.Status = 1;
                result.Message = "Requst Is Success";
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/Form/Delete Server Error";
            }
            return result;
        }

        /// <summary>
        /// 更新 数据
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultDto<dynamic> Update(GeneralUpdateRequest obj)
        {
            ResultDto<dynamic> result = new ResultDto<dynamic>();
            try
            {
                if (!Pickout.CheckTableColumnName(obj.TableName))
                {
                    result.FirstParam = false;
                    result.Status = 201;
                    result.Message = "TableName" + obj.TableName + " Invalid";
                }
                else if (obj == null || obj.Columns.Count == 0)
                {
                    result.FirstParam = false;
                    result.Status = 202;
                    result.Message = "Columns  Empty";
                }
                else if (obj == null || obj.AndOrLikeList.Count == 0)
                {
                    result.FirstParam = false;
                    result.Status = 202;
                    result.Message = "AndOrLike Empty";
                }

                Dictionary<string, string> keyValue = new Dictionary<string, string>();
                foreach (var item in obj.Columns)
                {
                    if (!Pickout.CheckTableColumnName(item.ColumnName))
                    {
                        result.FirstParam = false;
                        result.Status = 203;
                        result.Message = "ColumnName" + item.ColumnName + " Invalid";
                        return result;
                    }
                    keyValue.Add(item.ColumnName, item.ColumnValue);
                }
                List<TableAndOrLikeEntity> andOrLikeOrderList = new List<TableAndOrLikeEntity>();
                foreach (var item in obj.AndOrLikeList)
                {
                    if (!Pickout.CheckTableColumnName(item.ColumnName))
                    {
                        result.FirstParam = false;
                        result.Status = 204;
                        result.Message = "ColumnName" + item.ColumnName + " Invalid";
                        return result;
                    }
                    TableAndOrLikeEntity andOrLikeOrder = new TableAndOrLikeEntity();
                    andOrLikeOrder.AndOrLikeType = item.AndOrLikeType;
                    andOrLikeOrder.ColumnName = item.ColumnName;
                    andOrLikeOrder.Value = item.Value;
                    andOrLikeOrderList.Add(andOrLikeOrder);
                }
                var data = FormBLL.Instance.Update(obj.TableName, keyValue, andOrLikeOrderList);
                result.Status = 1;
                result.Message = "Requst Is Success";
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/Form/Update Server Error";
            }
            return result;
        }

        /// <summary>
        ///  获取 单一数据
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultDto<dynamic> Get(GeneralSignSearchRequest obj)
        {
            ResultDto<dynamic> result = new ResultDto<dynamic>();
            try
            {
                if (!Pickout.CheckTableColumnName(obj.TableName))
                {
                    result.FirstParam = false;
                    result.Status = 201;
                    result.Message = "TableName" + obj.TableName + " Invalid";
                }

                List<TableAndOrLikeEntity> andOrLikeOrderList = new List<TableAndOrLikeEntity>();
                foreach (var item in obj.AndOrLikeList)
                {
                    if (!Pickout.CheckTableColumnName(item.ColumnName))
                    {
                        result.FirstParam = false;
                        result.Status = 204;
                        result.Message = "ColumnName" + item.ColumnName + " Invalid";
                        return result;
                    }
                    TableAndOrLikeEntity andOrLikeOrder = new TableAndOrLikeEntity();
                    andOrLikeOrder.AndOrLikeType = item.AndOrLikeType;
                    andOrLikeOrder.ColumnName = item.ColumnName;
                    andOrLikeOrder.Value = item.Value;
                    andOrLikeOrderList.Add(andOrLikeOrder);
                }

                var data = FormBLL.Instance.Get(obj.TableName, andOrLikeOrderList);
                result.FirstParam = data;
                result.Status = 1;
                result.Message = "Requst Is Success";
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/Form/Get Server Error";
            }
            return result;
        }

    }
}
